package com.detrum.sportcamera;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bm.library.PhotoView;
import com.bumptech.glide.Glide;
import com.detrum.ShareSettingActivity;
import com.detrum.base.BaseActivity;
import com.detrum.bean.FileBean;
import com.detrum.bean.FileBeanAll;
import com.detrum.bean.ShareBean;
import com.detrum.sqlite.SqliteMediaOperation;
import com.detrum.sqlite.SqliteShareOperate;
import com.detrum.utils.LogUtils;
import com.detrum.utils.MyMethod;
import com.detrum.utils.ShareUtil;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class BigImageActivity extends BaseActivity {
    private Context context;
    private ViewPager viewPager;
    private MyPagerAdapter myPagerAdapter;
    private ArrayList<FileBean>beanList;
    private int position;
    private TextView share_btn;
    private Uri fileUri;
    private int flag;//跳转来源--1表示nfs,2表示本地
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_big_image);
        context = this;
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        myPagerAdapter = new MyPagerAdapter();
        viewPager.setAdapter(myPagerAdapter);
        viewPager.setCurrentItem(position);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int posi) {
                position = posi;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initView() {
        viewPager = findViewById(R.id.viewPager);
        share_btn = findViewById(R.id.share_btn);
        String json = getIntent().getStringExtra("json");
        position = getIntent().getIntExtra("position",-1);
        LogUtils.i(json);
        if(json!=null&&position!=-1){
            Gson gson = new Gson();
            FileBeanAll all = gson.fromJson(json, FileBeanAll.class);
            beanList = all.getBeanList();
            flag = all.getFlag();
        }
        if(flag==1){
            share_btn.setVisibility(View.GONE);
            return;
        }else if(flag==2){
            share_btn.setVisibility(View.VISIBLE);
            share_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // ShareUtil.shareFile(context, beanList.get(position).getCachePath());
                    fileUri = ShareUtil.getUri(context,beanList.get(position).getCachePath());
                    getShareList();
                }
            });
        }
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==31){
                MyMethod.ShowShareDialog(context,shareList,fileUri);
            }
        }
    };
    PackageManager pm;
    List<ResolveInfo>list;
    List<ShareBean> shareList;
    private void getShareList(){
        pm = getPackageManager();
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("image/*");
        list = pm.queryIntentActivities(intent,0);
        new Thread(new Runnable() {
            @Override
            public void run() {
                shareList = SqliteShareOperate.getInstence(context).SelectAll(1);
                if(shareList==null){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            MyMethod.toActivity(context, ShareSettingActivity.class);
                        }
                    });
                    return;
                }
                for (int i = 0; i < shareList.size(); i++) {
                    for (int j = 0; j < list.size(); j++) {
                        if(shareList.get(i).getName().equals(list.get(j).loadLabel(pm).toString())){
                            shareList.get(i).setIcon(list.get(j).loadIcon(pm));
                            break;
                        }
                    }
                }
                handler.sendEmptyMessage(31);
            }
        }).start();
    }

    class MyPagerAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return beanList==null?0:beanList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            PhotoView imageView = new PhotoView(context);
            imageView.setAdjustViewBounds(true);
            imageView.setMaxScale(2.0f);
            imageView.enable();
           /* ImageView imageView = new ImageView(context);
            imageView.setAdjustViewBounds(true);*/
            RelativeLayout.LayoutParams layoutParams=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
            imageView.setLayoutParams(layoutParams);
            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            Glide.with(context).load(beanList.get(position).getCachePath()).into(imageView);
            if(flag==1){
                imageView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        new AlertDialog.Builder(context,R.style.loading_dialog)
                                .setNegativeButton(R.string.download, new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                        new Thread(new Runnable() {
                                            @Override
                                            public void run() {
                                                SqliteMediaOperation.getInstence(context).UpdataItemLocal(1,beanList.get(position).getFileName());
                                            }
                                        }).start();
                                    }
                                })
                                .setPositiveButton(R.string.cancel, new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                })
                                .create()
                                .show();
                        return false;
                    }
                });
            }
            container.addView(imageView);
            return imageView;
        }


        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((ImageView) object);
        }
    }
}
